package me.zzp.domain;

import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhangzepeng
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Condition {
    private final static Gson gson = new Gson();

    private String operator;
    private List<? extends Object> parameters;

    public static Condition and(Condition... conditions) {
        return new Condition("and", Arrays.asList(conditions));
    }

    public static Condition or(Condition... conditions) {
        return new Condition("or", Arrays.asList(conditions));
    }

    public static Condition not(Condition condition) {
        return new Condition("not", Arrays.asList(condition));
    }

    public static Condition eq(String column, Object value) {
        return new Condition("=", Arrays.asList(column, value));
    }

    public static Condition ne(String column, Object value) {
        return new Condition("!=", Arrays.asList(column, value));
    }

    public static Condition gt(String column, Object value) {
        return new Condition(">", Arrays.asList(column, value));
    }

    public static Condition ge(String column, Object value) {
        return new Condition(">=", Arrays.asList(column, value));
    }

    public static Condition lt(String column, Object value) {
        return new Condition("<", Arrays.asList(column, value));
    }

    public static Condition le(String column, Object value) {
        return new Condition("<=", Arrays.asList(column, value));
    }

    public static Condition like(String column, String pattern) {
        return new Condition("like", Arrays.asList(column, pattern));
    }

    public static Condition notLike(String column, String pattern) {
        return new Condition("not like", Arrays.asList(column, pattern));
    }

    public static Condition between(String column, Object from, Object to) {
        return new Condition("between", Arrays.asList(column, from, to));
    }

    public static Condition notBetween(String column, Object from, Object to) {
        return new Condition("not between", Arrays.asList(column, from, to));
    }

    public static Condition in(String column, Object... values) {
        List<Object> parameters = new LinkedList<>();
        parameters.add(column);
        for (Object value : values) {
            parameters.add(value);
        }
        return new Condition("in", parameters);
    }

    public static Condition notIn(String column, Object... values) {
        List<Object> parameters = new LinkedList<>();
        parameters.add(column);
        for (Object value : values) {
            parameters.add(value);
        }
        return new Condition("not in", parameters);
    }

    public <T> T get(int index) {
        return (T)parameters.get(index);
    }

    @Override
    public String toString() {
        return gson.toJson(this);
    }
}
